<?php


class Asset_IndexController extends Zend_Controller_Action
{
    protected $_model;
    protected $_authService;
    protected $_userAssets;

    public function init()
    {
        
        $this->_authService = new Main_Service_Authentication();
        $this->_model   =   new Main_Model_User();
        
        if(!$this->_authService->getAuth()->getIdentity())
            $this->_redirect('/login');
        
        //zend login check
        //$userData = $this->_authService->getAuth()->getStorage()->read();
        //$user_id = $userData->uid
        //zend login check
        
        $layout = $this->_helper->layout();
        $layout->setLayout('dashboard');

        $this->_userAssets = new Asset_Model_Assetliability();

    }

    /**
        * function to display all the details of assets & liabilities added by a user
     */
    public function indexAction()
    {
        //if(!$this->_authService->getAuth()->hasIdentity())
        //   $this->_redirect('/');
        if(!$this->_authService->getAuth()->getIdentity())
            $this->_redirect('/login');
        
        $userData = $this->_authService->getAuth()->getStorage()->read();
        $userid = $userData->uid;
        
        $category_data  = new Asset_Model_Category();
        $subcat_data    = new Asset_Model_Subcategory();
        $label_data     = new Asset_Model_Label();
        //$form_field_data= new Asset_Model_AssetFormField();

        $this->view->catData        = $category_data->getAllCategoryArray();
        $this->view->subcatData     = $subcat_data->getAllSubcategoriesArray();
        $this->view->labelData      = $label_data->getAllLabelArray();
        //$this->view->formFieldData  = $form_field_data->getAllFormFieldsArray();

        //$userAssetData = new Asset_Model_AssetLiability();
        $userAssetData = new Asset_Model_Userassetdata();
        //$userid = 1;
        $temp_asset = $userAssetData ->getUserAssetDataByUserId($userid);
        //$this->view->userAssetData = $userAssetData ->getUserAssetDataByUserId($userid);
        
        $formatted_data = array();
        
        foreach($temp_asset as $key=>$val)
        {
            $formatted_data[$val['catid']][$val['subcatid']][$val['AssetCatLabelId']][$val['assetid']][] = $val;
        }
        
        $this->view->userAssetData = $formatted_data;
        
    }

    public function addassetAction()
    {
        //if(!$this->_authService->getAuth()->hasIdentity())
         //   $this->_redirect('/');

        $cat    = new Asset_Model_Category();
        $subcat = new Asset_Model_Subcategory();
        $label  = new Asset_Model_Label();
        $form_field_data = new Asset_Model_Formfield();
        
        $this->view->categories = $cat->getAllCategoryArray();
        $this->view->subcats    = $subcat->getAllSubcategoriesArray();
        $this->view->labels     = $label->getAllLabelArray();
        
        $this->view->formFieldData  = $form_field_data->getAllFormFieldsArray();
        
    }
    
    public function saveAction()
    {
        $request = $this->getRequest();
        if (!$request->isPost()) {
            return $this->_helper->redirector('index');
        }
        
//        if(!$this->_authService->getAuth()->getIdentity())
//            $this->_redirect('/login');
        
        $userData = $this->_authService->getAuth()->getStorage()->read();
        $userid = $userData->uid;
        
        $assetliabilityobj = new Asset_Model_Assetliability();
        $assetdetailsobj = new Asset_Model_Assetdetails();
        
        $response = $assetliabilityobj->saveAsset($request->getPost(),$userid);
        
        if (false === $response)
        {
            $this->_helper->FlashMessenger('')->setNamespace('error')->addMessage('An error occured');
            return $this->render('edit');
        }
        else
        {
            $val = $request->getPost();
            unset($val['hidden_cat_id']);
            unset($val['hidden_subcat_id']);
            unset($val['hidden_label_id']);
            unset($val['submitBtn']);
            $assetdetailsobj->saveAssetDetails($val, $response);
        }
        $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('Asset info saved');
        return $this->_redirect('/asset');
    }
    
    public function addLiabilityAction()
    {
        //if(!$this->_authService->getAuth()->hasIdentity())
         //   $this->_redirect('/');

        $cat = new Asset_Model_Category();
        $this->view->categories = $cat->getAllCategoryArray();

        $subcat = new Asset_Model_Subcategory();
        $this->view->subcat = $subcat->getAllSubcategoriesArray();
        
        $lab = new Asset_Model_Label();
        $this->view->labels = $lab->getAllLabelArray();

        $form_data = new Asset_Model_AssetLiability();

    }

    public function editassetAction()
    {
        //if(!$this->_authService->getAuth()->hasIdentity())
         //   $this->_redirect('/');
        $requst_params = $this->getRequest()->getParams();
        
        $assetid = $requst_params['assetid'];
        
        $this->view->tahir = $assetid;
        
        $asset_data = new Asset_Model_Assetdetails();
        $form_field_data = new Asset_Model_Formfield();
        
        $asset_details = $asset_data->getAssetDetails($assetid);
        
        $this->view->assetdata = $asset_details;
        
        $formfield_data = array();
        
        foreach($asset_details as $temp_val)
        {
            $formfield_data[$temp_val['assetid']][] = $form_field_data->getFormFieldsById($temp_val['FormFieldId']);      
        }
        
        $this->view->form_fields = $formfield_data;
        
    }
    
    function updateassetAction()
    {
        $request = $this->getRequest();
        
        if (!$request->isPost()) {
            $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('All fields are manadatory.');
            return $this->_helper->redirector('/my-assets');
        }
        
        $asset_obj = new Asset_Model_Assetdetails();
        $response = $asset_obj->updateAsset($request->getPost());
        // if rows updated then response will be 1, if same values goes then it will be 0 becauase db operation was successful.
        if($response == 1 || $response == 0)
        {
            return $this->_redirect('/my-assets');
        }    
    }
    
    public function editLiabilityAction()
    {
        //if(!$this->_authService->getAuth()->hasIdentity())
         //   $this->_redirect('/');

        $this->view->type="L";
    }
    
    public function deleteassetAction()
    {
        if(!$this->_authService->getAuth()->getIdentity())
            $this->_redirect('/login');
        
        $requst_params = $this->getRequest()->getParams();
        
        $assetid = $requst_params['assetid'];
        
        if( !$assetid )
            $this->_redirect('/my-assets');
        
        if($this->_userAssets->deleteItem($assetid))
            $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('Asset Deleted');
        else
            $this->_helper->FlashMessenger('')->setNamespace('Error')->addMessage('An error occured');

        return $this->_redirect('/my-assets');
        
    }

}
?>
